package o;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class dsq implements cdf, cdg, cdm {
    private static final List<String> ˊ = Collections.synchronizedList(new ArrayList());
    private static final Map<String, PackageInfo> ˋ = new ConcurrentHashMap();
    private static long ˎ = 0;
    private static Map<String, ApkUpgradeInfo> ॱ = new ConcurrentHashMap();
    private Application ˏ;
    private BroadcastReceiver ᐝ = new SafeBroadcastReceiver(this) {
        final /* synthetic */ dsq ॱ;

        {
            this.ॱ = r1;
        }

        public void onReceiveMsg(Context context, Intent intent) {
            if (intent == null || intent.getData() == null) {
                Logger.e("BaseAppDataManage", "error intent");
                return;
            }
            String schemeSpecificPart = intent.getData().getSchemeSpecificPart();
            if (TextUtils.isEmpty(schemeSpecificPart)) {
                Logger.e("BaseAppDataManage", "error packageName");
                return;
            }
            String action = intent.getAction();
            Logger.ˎ("BaseAppDataManage", " apkChangedReceiver, action = " + action + "," + "packageName:" + schemeSpecificPart);
            fhz ˋ = fho.ˏ().ˋ("PackageManager");
            if (ˋ != null) {
                cnv o_cnv = (cnv) ˋ.ॱ(cnv.class);
                if (o_cnv != null) {
                    o_cnv.ˏ(context, intent, 1);
                }
            }
            if ("android.intent.action.PACKAGE_ADDED".equals(action)) {
                ((cdd) cdb.ॱ(cdd.class)).ˏ(schemeSpecificPart, 1);
                this.ॱ.ˏॱ(schemeSpecificPart);
            } else if ("android.intent.action.PACKAGE_REMOVED".equals(action)) {
                if (intent.getBooleanExtra("android.intent.extra.REPLACING", false)) {
                    Logger.ˎ("BaseAppDataManage", "replace app,receive REMVED Broadcast");
                    ((cdd) cdb.ॱ(cdd.class)).ˏ(schemeSpecificPart, 5);
                    return;
                }
                dsr.ˊ().ˎ(schemeSpecificPart);
                ((cdd) cdb.ॱ(cdd.class)).ˏ(schemeSpecificPart, 2);
                this.ॱ.ͺ(schemeSpecificPart);
            } else if ("android.intent.action.PACKAGE_CHANGED".equals(action)) {
                ((cdd) cdb.ॱ(cdd.class)).ˏ(schemeSpecificPart, 3);
                this.ॱ.ʻ(schemeSpecificPart);
            }
        }
    };

    static final class b extends AsyncTask<Void, Void, Void> {
        private String ˊ;

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˋ((Void[]) objArr);
        }

        public b(String str) {
            this.ˊ = str;
        }

        protected Void ˋ(Void... voidArr) {
            PackageInfo ˏ = dnx.ˏ(this.ˊ, ContentManager.getInstant().getContext());
            if (ˏ != null) {
                dsq.ˋ.put(this.ˊ, ˏ);
                if (!dti.ˏ(ContentManager.getInstant().getContext(), this.ˊ)) {
                    dsq.ˊ.remove(this.ˊ);
                } else if (!dsq.ˊ.contains(this.ˊ)) {
                    dsq.ˊ.add(this.ˊ);
                }
            }
            ((cdd) cdb.ॱ(cdd.class)).ˏ(1, this.ˊ, 1);
            return null;
        }
    }

    static final class c extends AsyncTask<Void, Void, Void> {
        private String ˊ;

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ॱ((Void[]) objArr);
        }

        public c(String str) {
            this.ˊ = str;
        }

        protected Void ॱ(Void... voidArr) {
            dsq.ˋ.remove(this.ˊ);
            dsq.ˊ.remove(this.ˊ);
            ((cdd) cdb.ॱ(cdd.class)).ˏ(1, this.ˊ, 2);
            return null;
        }
    }

    public final void ॱ(Application application) {
        this.ˏ = application;
        new dso().executeOnExecutor(dso.ˊ, new Void[0]);
    }

    public void ˎ() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.PACKAGE_ADDED");
        intentFilter.addAction("android.intent.action.PACKAGE_REMOVED");
        intentFilter.addAction("android.intent.action.PACKAGE_CHANGED");
        intentFilter.addDataScheme(HiAnalyticsConstant.BI_KEY_PACKAGE);
        try {
            this.ˏ.registerReceiver(this.ᐝ, intentFilter);
        } catch (Throwable e) {
            Logger.ˋ("BaseAppDataManage", "monitorApkChanged(Context context) ", e);
        }
    }

    public void ˏ() {
        this.ˏ.unregisterReceiver(this.ᐝ);
    }

    public int ˋ(@NonNull String str) {
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            coa o_coa = (coa) ˋ.ॱ(coa.class);
            if (o_coa != null) {
                cnz ˏ = o_coa.ˏ(str);
                if (ˏ == cnz.INSTALLING) {
                    return 11;
                }
                if (ˏ == cnz.WAIT_INSTALL) {
                    return 10;
                }
                if (ˏ == cnz.WAIT_UNINSTALL) {
                    return 12;
                }
                if (ˏ == cnz.UNINSTALLING) {
                    return 13;
                }
                if (ˏ == cnz.UNINSTALLED) {
                    return 1;
                }
                if (ˏ == cnz.UNINSTALLED_PREDL) {
                    return 2;
                }
            }
        }
        PackageInfo packageInfo = (PackageInfo) ˋ.get(str);
        ApkUpgradeInfo ˎ = dss.ॱ().ˎ(str, false, 0);
        if (ˎ == null) {
            ˎ = dss.ॱ().ॱ(str, false, 0);
        }
        if (!(packageInfo == null || r2 == null || packageInfo.versionCode < r2.ʿ())) {
            ˎ = null;
        }
        if (ˎ != null) {
            return ˊ(ˎ);
        }
        if (packageInfo != null) {
            return 0;
        }
        return -2;
    }

    private int ˊ(ApkUpgradeInfo apkUpgradeInfo) {
        if (apkUpgradeInfo.ʼ() > 0) {
            return 4;
        }
        return 3;
    }

    public boolean ॱॱ(@NonNull String str) {
        return ˋ.containsKey(str);
    }

    public boolean ˎ(@NonNull String str, int i) {
        PackageInfo packageInfo = (PackageInfo) ˋ.get(str);
        return packageInfo != null && packageInfo.versionCode >= i;
    }

    public void ˊ(@NonNull Collection<ApkUpgradeInfo> collection) {
        ॱ.clear();
        for (ApkUpgradeInfo apkUpgradeInfo : collection) {
            ॱ.put(apkUpgradeInfo.ᐝ(), apkUpgradeInfo);
        }
        dth.ॱ().ˏ();
        dth.ॱ().ˊ(new ArrayList(collection));
    }

    public boolean ʽ(@NonNull String str) {
        return ˊ.contains(str);
    }

    private void ʻ(final String str) {
        new AsyncTask<Void, Void, Void>(this) {
            final /* synthetic */ dsq ˏ;

            protected /* synthetic */ Object doInBackground(Object[] objArr) {
                return ˋ((Void[]) objArr);
            }

            protected Void ˋ(Void... voidArr) {
                if (!dti.ˏ(this.ˏ.ˏ, str)) {
                    dsq.ˊ.remove(str);
                } else if (!dsq.ˊ.contains(str)) {
                    dsq.ˊ.add(str);
                }
                ((cdd) cdb.ॱ(cdd.class)).ˏ(1, str, 3);
                return null;
            }
        }.executeOnExecutor(dso.ˊ, new Void[0]);
    }

    private void ͺ(@NonNull String str) {
        new c(str).executeOnExecutor(dso.ˊ, new Void[0]);
    }

    private void ˏॱ(String str) {
        new b(str).executeOnExecutor(dso.ˊ, new Void[0]);
    }

    public void ॱॱ() {
        List<PackageInfo> ˊ = dnx.ˊ(this.ˏ, true);
        if (ˊ != null) {
            ˋ.clear();
            for (PackageInfo packageInfo : ˊ) {
                if (packageInfo != null) {
                    ˋ.put(packageInfo.packageName, packageInfo);
                }
            }
        }
    }

    public void ʽ() {
        for (PackageInfo packageInfo : ˋ.values()) {
            if (!(packageInfo == null || !dti.ˏ(this.ˏ, packageInfo.packageName) || ˊ.contains(packageInfo.packageName))) {
                ˊ.add(packageInfo.packageName);
            }
        }
    }

    public void ʼ() {
        try {
            fhz ˋ = fho.ˏ().ˋ("PackageManager");
            if (ˋ != null) {
                cny o_cny = (cny) ˋ.ॱ(cny.class);
                o_cny.ॱ(ContentManager.getInstant().getContext(), ˋ);
                Logger.ˎ("BaseAppDataManage", "uninstalled apk:" + o_cny.ˏ(ContentManager.getInstant().getContext()).size());
            }
        } catch (Throwable e) {
            Logger.ˋ("BaseAppDataManage", "initDownloadedApkData failed.", e);
        }
    }

    public void ˊॱ() {
        dss.ॱ().ʽ();
    }

    public void ͺ() {
        dss.ॱ().ॱॱ();
    }

    public void ˏॱ() {
        dss.ॱ().ᐝ();
    }

    public void ˋॱ() {
        dss.ॱ().ʻ();
    }

    public void ॱˊ() {
        ॱ.clear();
        for (ApkUpgradeInfo apkUpgradeInfo : dth.ॱ().ˎ()) {
            ॱ.put(apkUpgradeInfo.ᐝ(), apkUpgradeInfo);
        }
    }

    public boolean ʼ(@NonNull String str) {
        return dss.ॱ().ˊ(str);
    }

    public void ˎ(@NonNull String str) {
        ͺ(str);
    }

    public void ˊ(@NonNull String str) {
        ˏॱ(str);
    }

    public void ˏ(@NonNull String str) {
        PackageInfo ˏ = dnx.ˏ(str, ContentManager.getInstant().getContext());
        if (ˏ != null) {
            ˋ.put(str, ˏ);
            if (!dti.ˏ(ContentManager.getInstant().getContext(), str)) {
                ˊ.remove(str);
            } else if (!ˊ.contains(str)) {
                ˊ.add(str);
            }
        }
        ((cdd) cdb.ॱ(cdd.class)).ˏ(1, str, 1);
    }

    public void ॱ() {
        new AsyncTask<Void, Void, Void>(this) {
            final /* synthetic */ dsq ॱ;

            {
                this.ॱ = r1;
            }

            protected /* synthetic */ Object doInBackground(Object[] objArr) {
                return ॱ((Void[]) objArr);
            }

            protected Void ॱ(Void... voidArr) {
                int i = (dsq.ˎ == 0 || System.currentTimeMillis() - dsq.ˎ > 10000) ? 1 : 0;
                if (i != 0) {
                    this.ॱ.ॱॱ();
                    this.ॱ.ʽ();
                    ((cdd) cdb.ॱ(cdd.class)).ˏ(1, null, 4);
                    Logger.ˎ("BaseAppDataManage", "refresh Installed List, total size:" + dsq.ˋ.size());
                }
                return null;
            }
        }.executeOnExecutor(dso.ˊ, new Void[0]);
    }

    public void ॱ(@NonNull String str) {
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            cny o_cny = (cny) ˋ.ॱ(cny.class);
            if (o_cny != null) {
                o_cny.ˊ(ContentManager.getInstant().getContext(), str);
            }
        }
    }

    public void ˋ() {
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            cny o_cny = (cny) ˋ.ॱ(cny.class);
            if (o_cny != null) {
                o_cny.ˎ(ContentManager.getInstant().getContext());
            }
        }
    }

    @Nullable
    public PackageInfo ᐝ(@NonNull String str) {
        return (PackageInfo) ˋ.get(str);
    }

    public int ˊ() {
        return ॱ.size();
    }

    public List<ApkUpgradeInfo> ᐝ() {
        return new ArrayList(ॱ.values());
    }

    @NonNull
    public List<PackageInfo> ʻ() {
        return new ArrayList(ˋ.values());
    }

    public boolean ˏ(@NonNull String str, boolean z, int i) {
        return dss.ॱ().ˎ(str, z, i) != null;
    }

    @Nullable
    public ApkUpgradeInfo ˊ(@NonNull String str, boolean z, int i) {
        return dss.ॱ().ˎ(str, z, i);
    }

    public int ˊ(boolean z, int i) {
        return dss.ॱ().ˋ(z, i);
    }

    public boolean ˎ(@NonNull String str, boolean z, int i) {
        return dss.ॱ().ॱ(str, z, 0) != null;
    }

    @Nullable
    public ApkUpgradeInfo ˋ(@NonNull String str, boolean z, int i) {
        return dss.ॱ().ॱ(str, z, i);
    }
}
